<?php

/**
 * Created by PhpStorm.
 * User: My pc
 * Date: 2017/2/4/004
 * Time: 15:07
 */
class AdverAction extends Action {

    // 初始化
    public function __construct(&$_tpl) {
        parent::__construct($_tpl,new AdverModel());
    }

    //action
    public function _action() {
        switch ($_GET['action']) {
            case 'show' :
                $this->show();
                break;
            case 'add' :
                $this->add();
                break;
            case 'update' :
                $this->update();
                break;
            case 'delete' :
                $this->delete();
                break;
            case 'state' :
                $this->state();
                break;
            default:
                Tool::alertBack('非法操作！');
        }
    }

    // show
    private function show() {
        parent::page($this->_model->getAdverTotal());
        $this->_tpl->assign('show',true);
        $this->_tpl->assign('title','广告列表');
        $_object = $this->_model->getAllAdver();
        Tool::subStr($_object,'link',20,'utf-8');
        if ($_object) {
            foreach ($_object as $_value) {
                switch ($_value->type) {
                    case 1 :
                        $_value->type = '文字广告';
                        break;
                    case 2 :
                        $_value->type = '头部广告690x80';
                        break;
                    case 3 :
                        $_value->type = '侧栏广告270x200';
                        break;
                }
                if (empty($_value->state)) {
                    $_value->state = '<span class="red">[否]</span> | <a href="adver.php?action=state&type=ok&id='.$_value->id.'">确定</a>';
                } else {
                    $_value->state = '<span class="green">[是]</span> | <a href="adver.php?action=state&type=cancel&id='.$_value->id.'">取消</a>';
                }
            }
        }
        $this->_tpl->assign('AllAdver',$_object);
    }

    //state
    private function state() {
        if (isset($_GET['id'])) {
            $this->_model->id = $_GET['id'];
            if (!$this->_model->getOneAdver()) Tool::alertBack('警告：不存在此广告！');
            if ($_GET['type'] == 'ok') {
                $this->_model->setStateOK() ? Tool::alertLocation(null,PREV_URL) : Tool::alertBack('警告：设置轮播失败！');
            } elseif ($_GET['type'] == 'cancel') {
                $this->_model->setStateCancel() ? Tool::alertLocation(null,PREV_URL) : Tool::alertBack('警告：取消轮播失败！');
            } else {
                Tool::alertBack('警告：非法操作！');
            }
        } else {
            Tool::alertBack('警告：非法操作！');
        }
    }

    // add
    private function add() {
        if (isset($_POST['send'])) {
            if (Validate::checkNull($_POST['title'])) Tool::alertBack('警告：标题不得为空！');
            if (Validate::checkLength($_POST['title'],2,'min')) Tool::alertBack('警告：标题长度不得小于两位！');
            if (Validate::checkLength($_POST['title'],20,'max')) Tool::alertBack('警告：标题长度不得大于二十位！');
            if (Validate::checkNull($_POST['link'])) Tool::alertBack('警告：链接不得为空！');
            if ($_POST['type'] == '2' || $_POST['type'] == '3') {
                if (Validate::checkNull($_POST['thumbnail'])) Tool::alertBack('警告：广告图片不得为空！');
            }
            if (Validate::checkLength($_POST['info'],200,'max')) Tool::alertBack('警告：描述长度不得大于两百位！');
            $this->_model->title = $_POST['title'];
            $this->_model->type = $_POST['type'];
            $this->_model->thumbnail = $_POST['thumbnail'];
            $this->_model->link = $_POST['link'];
            $this->_model->info = $_POST['info'];
            $this->_model->addAdver() ? Tool::alertLocation('恭喜，新增广告成功！','?action=show') : Tool::alertBack('很遗憾，新增广告失败！');
        }
        $this->_tpl->assign('add',true);
        $this->_tpl->assign('title','新增广告');
        $this->_tpl->assign('prev_url',PREV_URL);
    }

    // update
    private function update() {
        if (isset($_GET['id'])) {
            $this->_model->id = $_GET['id'];
            $_adver = $this->_model->getOneAdver();
            if (!$_adver) Tool::alertBack('警告：不存在此广告！');
            $this->_tpl->assign('id',$_adver->id);
            $this->_tpl->assign('titlec',$_adver->title);
            $this->_tpl->assign('info',$_adver->info);
            $this->_tpl->assign('link',$_adver->link);
            $this->_tpl->assign('prev_url',PREV_URL);
            $this->_tpl->assign('update',true);
            $this->_tpl->assign('title','修改广告');
        } else {
            Tool::alertBack('非法操作！');
        }
    }

    // delete
    private function delete() {
        if (isset($_GET['id'])) {
            $this->_model->id = $_GET['id'];
            $this->_model->deleteAdver() ? Tool::alertLocation('恭喜你，删除广告成功！', PREV_URL) : Tool::alertBack('很遗憾，删除广告失败！');
        } else {
            Tool::alertBack('非法操作！');
        }
    }
}